#include <QPicture>
#include "qrcodewidget.h"
#include "ui_qrcode.h"
#include "qrencode.h"
#include "qrenc.c"
#include <QFileDialog>

QRCodeWidget::QRCodeWidget(QWidget *parent) :
    QWidget(parent, Qt::Dialog),
    ui(new Ui::QRCode)
{
//    QTextCodec *codec = QTextCodec::codecForName("utf8");
//    QTextCodec::setCodecForLocale(codec);
//    QTextCodec::setCodecForCStrings(codec);
//    QTextCodec::setCodecForTr(codec);
    ui->setupUi(this);
}

QRCodeWidget::~QRCodeWidget()
{
    delete ui;
}

void QRCodeWidget::on_pushButton_clicked()
{
    QString info = ui->lineEdit->text();
    if (info.isEmpty()) {
        return;
    }
    char outfile[] = "output.png";
    margin = 2;
    ::size = 7;
    version = 2;
    //QRcode *qrcode = QRcode_encodeString(info.toStdString().data(), 2, QR_ECLEVEL_L, QR_MODE_8, 0);
    //writePNG(qrcode, outfile);

    qrencode((unsigned char *)info.toStdString().data(), info.length(), outfile);
    ui->label_3->setPixmap(QPixmap(outfile));
}

void QRCodeWidget::on_pushButton_2_clicked()
{
    const QPixmap *p;
    QString path;
    p = ui->label_3->pixmap();
    path = QFileDialog::getSaveFileName(this, tr("Save File"),
                               QApplication::applicationDirPath(),
                               tr("Images (*.png)"));
    p->save(path);

}
